<?php

    function validation($field_name){
        if (isset($_POST[$field_name]) && strlen($_POST[$field_name]) > 0 && $_POST[$field_name] != '')
            return TRUE;
        else
            return FALSE;
    }

    function show_error($message){
        echo "<p><strong>ERROR:</strong> $message </p>";
    }

    function pricing($base_price, $number_of_toppings){
        $cost = $number_of_toppings*.50 + $base_price;
        return $cost;
    }

    if (isset($_POST['fname']) && isset($_POST['lname'])){
        //Prepare the input
        $fname = trim($_POST['fname']);
        $lname = trim($_POST['lname']);
        $addy = trim($_POST['addy']);
        $city = $_POST['city'];
        $phone = trim($_POST['phone']);
        $sandwich  = $_POST['sandwich_type'];

        //validation of fields
        if ( ! validation('fname') OR ! validation('lname')){
            $message = "Please check the name fields. One of them is missing information.";
            show_error($message);
            exit();
        }
        if (! validation('addy') OR ! validation('city')){
            $message = "Please check the address fields. One of them is missing information.";
            show_error($message);
            exit();
        }
        if (! validation('phone')){
            $message = "Please enter a phone number for contact purposes.";
            show_error($message);
            exit();
        }
        //special validation for sandwich type
        if (! isset($_POST['sandwich_type']) && $_POST['sandwich_type'] != ''){
            $message = "Please choose a sandwhich type.";
            show_error($message);
            exit();
        }

        //check to see if any topping options are chosen
        $toppings = array();

        if(isset($_POST['lettuce']))
            $toppings[] = "Lettuce";
        if(isset($_POST['tomatoe']))
            $toppings[]= "Tomatoe";
        if(isset($_POST['onion']))
            $toppings[] = "Onion";
        if(isset($_POST['sweet']))
            $toppings[] = "Sweet Peppers";
        if(isset($_POST['mayo']))
            $toppings[] = "Mayo";
        if(isset($_POST['y_mustard']))
            $toppings[] = "Yellow Mustard";
        if(isset($_POST['s_mustard']))
            $toppings[] = "Spicy Mustard";
    }
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if (isset($message))
            echo $message;
        else{
            echo "<h1>Thank you for your order!</h1>";
            echo "<p><strong>Name:</strong> $fname $lname</p>";
            echo "<p><strong>Address:</strong> $addy, $city";
            echo "<p><strong>Phone:</strong> $phone";
            echo "<p><strong>Sandwich:</strong> $sandwich</p>";
            echo "<p><strong>Toppings:</strong> </p>";
            echo "<p><strong>Beverage:</strong> </p>";
            echo "<p><strong>Comments:</strong> </p>";
        }
        ?>
    </body>
</html>